package com.cc.learning.controller;

import com.cc.learning.po.UserInfoPo;
import com.cc.learning.service.UserInfoService;
import com.cc.learning.service.impl.UserInfoServiceImpl;
import com.fesine.commons.util.CryptographyUtil;
import com.fesine.commons.util.ResponseUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/7.
 */
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
        private static final Logger logger = Logger.getLogger(UserInfoController.class);

        @Autowired
        private UserInfoService userInfoService;
        public static long initTime;
        public static long loginTime;

         @Value("${upload.file.root}")
         private String uploadFileRoot;
        /*登录*/
        public  void login(HttpServletRequest request, HttpServletResponse response)  {
        //验证验证
            Map<String,Object> map = new HashMap();
            String userName=request.getParameter("userName");
            String password = request.getParameter("password");
            String token = ((String) request.getSession().getAttribute("captchaToken")).toLowerCase();
            String checkCode=((String)request.getParameter("checkCode")).toLowerCase();
            if(!token.equals(checkCode)){
                map.put("success",false);
                map.put("msg","验证码不匹配");
            }else{
            //从数据库读取用户信息
                UserInfoPo userInfoPo = new UserInfoPo();
                userInfoPo.setUserName(userName);
                UserInfoPo oldUserInfoPo =userInfoService.getUserInfo(userInfoPo);
                if (oldUserInfoPo==null){
                    map.put("success",false);
                    map.put("msg","用户信息不存在");
                }
                //用户信息存在，验证密码
                if(!CryptographyUtil.md5(password,"cc").equals(oldUserInfoPo.getPassword())){
                    map.put("success",false);
                    map.put("msg","密码不匹配");
                }else {
                    //登录成功，用户名放入session
                    request.getSession().setAttribute("userName",userName);
                    map.put("success",true);
                    map.put("msg","登录成功");
                }
                try {
                    ResponseUtils.outPut(response,map);
                } catch (Exception e) {
                    logger.error("登录放入response失败");
                }
            }
    }
        /*退出登录*/
        @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response){
        //清空session 返回成功
            request.getSession().invalidate();
            Map<String,Object> map = new HashMap();
        map.put("success",true);
        map.put("msg","退出成功");

//        将信息放入response当中返回
        try {
            ResponseUtils.outPut(response,map);
        } catch (Exception e) {
            logger.error("放入response错误");
        }
    }
    /*新增用户信息*/
    @RequestMapping(value = "/addUserInfo", method = RequestMethod.POST)
    public void addUserInfo(HttpServletRequest request,HttpServletResponse response){
    UserInfoPo userInfoPo = setUserInfoPo(request);
    userInfoService.addUserInfo(userInfoPo);
        Map<String,Object> map = new HashMap();
        map.put("success",true);
        map.put("msg","新增用户信息成功!");
        try {
            ResponseUtils.outPut(response,map);
        } catch (Exception e) {
            logger.error("新增用户信息失败! ");
        }
    }
        /*更新用户数据*/
    public void updateUserInfo(HttpServletRequest request,HttpServletResponse response){
        UserInfoPo userInfoPo = setUserInfoPo(request);
        userInfoService.modifyUserInfo(userInfoPo);
        Map<String,Object> map = new HashMap<String ,Object>();
        map.put("success",true);
        map.put("msg","更新用户数据成功");
        try {
            ResponseUtils.outPut(response,map);
        } catch (Exception e) {
            logger.error("更新用户数据失败");
        }

    }

    public  void  deleteUserInfo(HttpServletRequest request,HttpServletResponse response){
    }

    public void  selectUserInfoList(){
    }



        /*根据输入项放入Po当中*/
    private UserInfoPo setUserInfoPo(HttpServletRequest request) {
    // TODO: 2017/4/8
        UserInfoPo userInfoPo = new UserInfoPo();
        userInfoPo.setId(request.getParameter("id"));
        userInfoPo.setUserName(request.getParameter("userName"));
        if (request.getParameter("password")!=null) {
            userInfoPo.setPassword(CryptographyUtil.md5(request.getParameter("password"),"cc"));
        }
            return userInfoPo;
    }


}
